package ${model.packageName}.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import net.sf.json.JSONObject;

import com.dhc.framework.auth.usercolsetting.model.UserColSetting;
import com.dhc.framework.auth.usercolsetting.service.EabaColEAS;
import com.dhc.framework.base.exception.BaseASException;
import com.dhc.framework.base.util.HttpError;
import com.dhc.framework.base.util.SpringUtils;
import com.dhc.framework.util.ComsSessionUtils;
import com.dhc.sms.common.common.as.CommonAS;
import com.dhc.sms.rep.common.ExcelWorker;
import com.dhc.sms.rep.common.MyBaseSearchForm;
import com.dhc.sms.rep.common.RepCommon;
import com.dhc.sms.rep.common.RepCommonUtils;
import com.dhc.sms.rep.common.ReportQueueWorker;
import com.dhc.sms.rep.common.SearchCondition;
import ${model.packageName}.as.${model.initUpperName}AS;
import ${model.packageName}.domain.${model.initUpperName};
import ${model.packageName}.domain.${model.initUpperName}TableData;

import com.retail.web.BaseLogAction;

/**
 *	${model.desc}
 */
public class ${model.initUpperName}Action extends BaseLogAction<${model.initUpperName}AS> {

	private static String[] reportHeader = new String[] {<#list model.fields as field>"${field.desc}"<#sep>,</#sep></#list>};
	private static String reportName = "${model.desc}";// 报表名称
	private static String reportType = "${model.initLowerName}";// 报表模块名称
	private static String reportExportMethod = "doEexcel";// 文件导出方法

	/**
	 * 初始化数据
	 */ 
	public void setDataContext(HttpServletRequest request) {
	
	}
	/**
	 * 解析提交表单到数据对象
	 */
	public ${model.initUpperName} resovleQueryPara(HttpServletRequest request, ActionForm form) {
		${model.initUpperName}Form mform = (${model.initUpperName}Form) form;
		${model.initUpperName} model = new ${model.initUpperName}();
		SpringUtils.copyProperties(model, mform);
		return model;
	}
	/**
	 * 查询列表数据
	 */	
	public ActionForward toList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		return super.doQuery(mapping, form,  request, response);
	}
	/**
	 * 查看明细数据
	 */
	public ActionForward toDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		${model.initUpperName} model = resovleQueryPara(request, form);
		${model.initUpperName} ${model.initLowerName} = this.getDefaultAppService().queryOne(model);
		request.setAttribute("${model.initLowerName}", ${model.initLowerName});
		return mapping.findForward("detail");
	} 
	/**
	 * 编辑明细数据
	 */ 
	public ActionForward toEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		${model.initUpperName} model = resovleQueryPara(request, form);
		String type = request.getParameter("type");
		if(!StringUtils.equals(type, "add")){
			${model.initUpperName} ${model.initLowerName} = this.getDefaultAppService().queryOne(model);
			request.setAttribute("${model.initLowerName}", ${model.initLowerName});
		}
		request.setAttribute("type", type);
		return mapping.findForward("edit");
	} 
	/**
	 * 保存数据
	 */
	public ActionForward doSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		${model.initUpperName} model = resovleQueryPara(request, form);
		try{
			String type = request.getParameter("type");
			if(StringUtils.equals(type, "insert")){
				this.getDefaultAppService().add(model);
			}
			else{
				this.getDefaultAppService().edit(model);
			}
			HttpError.warn(request, "保存成功");
		}
		catch(Exception e){
			log.error(ExceptionUtils.getStackTrace(e));
			HttpError.warn(request, "保存失败");
		}
		return toEdit(mapping, form,  request, response);
	}
	/**
	 * 删除数据
	 * 返回Ajax数据
	 */
	public ActionForward doDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		JSONObject resultObj = new JSONObject();
		${model.initUpperName} model = resovleQueryPara(request, form);
		try{
			this.getDefaultAppService().delete(model);
			resultObj.put("result", "success");
		}
		catch(Exception e){
			log.error(ExceptionUtils.getStackTrace(e));
			resultObj.put("result", "failed");
			resultObj.put("message", e.getLocalizedMessage());
		}
		writeAjaxStream(response, resultObj.toString());
		return null;
	}
	/**
	 * 请求导出数据
	 */
	public ActionForward toExport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		return null;
	}
	/**
	 * 导出数据
	 */
	public ActionForward doEexcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		return null;
	}
}
